/**
 * jquery.imgToggle.js 
 * 
 * 图片横向滚动
 * created by Oscar at 2017/05/23 22:46.
 * 
 */
; (function (J) {
	/// <summary>图片横向滚动(非无缝对接，仅支持左右切换)</summary>
	/// <param name="options" type="Object">对象参数,可选参数：
	/// <br/>prev：选择器或对象,默认.prev
	/// <br/>next：选择器或对象,默认.next
	/// <br/>
	/// <br/>speed：Int,图片滑动速度,越小越快,默认1000
	/// <br/>auto：Boolean,是否自动切换,默认true
	/// <br/>time：Int,自动切换时间(秒),默认3秒
	/// </param>
	J.fn.imgToggle = function (options) {
		var oThis = this;
		var defaults = {
			body: '.m_thumb_lst',
			prev: '.u_arrow_left',
			next: '.u_arrow_right',
			width: null,

			speed: 1000,
			auto: false,
			time: 3,
			minCount: 5,  // 最小项目数量
			completed: null   // 动画完成时
		};

		if (options) { J.extend(defaults, options); }

		var oUl = J(defaults.body, oThis);
		var oItems = oUl.find("li");
		var isPause = false;
		var iWidth = parseInt(defaults.width) || oItems.eq(0).outerWidth(true);
		var iLen = oItems.length;
		var iNum = 0;
		var iTimer = null;
		var currentToggleClass = oUl.data("current-class");

		// 初始化
		this.init = function () {

			// 是否启用上下点击
			if (oItems.length > defaults.minCount) {
				J(oThis).addClass(J(oThis).data("enabled-class"));
			}

			if (defaults.prev != null) {
				if (typeof defaults.prev === 'string') {
					defaults.prev = J(defaults.prev, oThis);
				}
				defaults.prev.click(oThis.prev);
			}
			if (defaults.next != null) {
				if (typeof defaults.next === 'string') {
					defaults.next = J(defaults.next, oThis);
				}
				defaults.next.click(oThis.next);
			}

			oThis.start();
		};
		// 获得当前索引
		this.getCurrentIndex = function () {
			return J(oThis).find("li." + currentToggleClass).index();
		};
		// 设置索引
		this.setCurrentIndex = function (index) {
			oItems.eq(index).addClass(currentToggleClass).siblings().removeClass(currentToggleClass);
		};
		// 是否允许移动
		this.isMove = function (direction) {
			if (iNum >= 0 && iNum < iLen) {
				if (direction == 'prev') {
					if (((iNum + 1) % defaults.minCount) == 0)
						return true;
				}
				else if (direction == 'next') {
					if ((iNum % defaults.minCount) == 0)
						return true;
				}
			}

			return false;
		};
		// 后退一步是人生
		this.prev = function () {
			iNum = oThis.getCurrentIndex();

			if (!oUl.is(":animated")) {
				iNum--;

				if (iNum <= 0)
					iNum = 0;

				oThis.move('prev');
			}
		};
		// 往前一步是黄昏
		this.next = function () {
			iNum = oThis.getCurrentIndex();

			if (!oUl.is(":animated")) {
				
				iNum++;

				if (iNum >= iLen)
					iNum = iLen - 1;

				oThis.move('next');
			}
		};
		// 移动
		this.move = function (direction) {
			if (!oUl.is(":animated")) {
				oThis.stop();
				
				oThis.setCurrentIndex(iNum);

				if (defaults.completed)
					defaults.completed(iNum);

				if (oThis.isMove(direction)) {
					var iLeft = iNum * iWidth;

					if (direction == 'prev')
						iLeft = Math.floor(iNum / defaults.minCount) * defaults.minCount * iWidth;

					oUl.animate({ "left": -iLeft + "px" }, defaults.speed, function () {
						oThis.start();
					});
				}
			}
		};
		// 联通
		this.start = function () {
			if (defaults.auto) {
				iTimer = setInterval(function () {
					if (!isPause) {

						if (iNum >= iLen)
							iNum = 0;

						iNum++;

						oThis.move();
					}
				}, defaults.time * 1000);
			}
		};
		// 电信
		this.stop = function () {
			if (!!iTimer)
				clearInterval(iTimer);
		};

		return this.each(function () {
			oThis.init();
		});
	};
})(jQuery);